﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.talent.dao.impl;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.talent.dao.*;
import com.hh.bss.talent.entity.*;
import com.hh.bss.talent.service.*;
import com.hh.bss.talent.vo.query.*;
import com.hh.bss.util.*;





import static com.hh.bss.util.ObjectUtils.*;

import org.springframework.stereotype.Repository;

import sun.security.krb5.internal.crypto.dk.ArcFourCrypto;

@Repository
public class CvWorkDaoImpl extends BaseHibernateDao<CvWork,java.lang.Integer>  implements CvWorkDao<CvWork,java.lang.Integer>{

	public Class getEntityClass() {
		return CvWork.class;
	}
	
	public Page findPage(CvWorkQuery query) {
        //XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接 
        // [column] 为PageRequest的属性
        
		
		    Class[] os={CvWorkQuery.class};
    		Class property= org.springframework.beans.BeanUtils.findPropertyType(query.getField_type(), os);
        if (property!=null){
        	 if ("java.lang.String".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(),query.getQuery());
	    			
	    		}
	    		else if ("java.lang.Integer".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseInteger(query.getQuery(), 0));
	    			
	    		}
	    		else if ("java.lang.Double".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseDouble(query.getQuery(), 0));
	    		}
	    		else if ("java.util.Date".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), DateHelper.toDate(query.getQuery(), DateHelper.DEFAULT_DATE_FORMAT));
	    		}
        }	 
        //生成sql2的原因是为了不喜欢使用xsqlbuilder的同学，请修改生成器模板，删除本段的生成
        StringBuilder sql2 = new StringBuilder("select t from CvWork t where 1=1 ");
          if(isNotEmpty(query.getId())) {
            sql2.append(" and  t.id = :id ");
        }
          if(isNotEmpty(query.getCvId())) {
            sql2.append(" and  t.cvId = :cvId ");
        }
          if(isNotEmpty(query.getIsEng())) {
              sql2.append(" and  t.isEng = :isEng ");
          }
//        if(isNotEmpty(query.getBeginDBegin())) {
//            sql2.append(" and  t.beginD >= :beginDBegin ");
//        }
//        if(isNotEmpty(query.getBeginDEnd())) {
//            sql2.append(" and  t.beginD <= :beginDEnd ");
//        }
//        if(isNotEmpty(query.getEndDBegin())) {
//            sql2.append(" and  t.endD >= :endDBegin ");
//        }
//        if(isNotEmpty(query.getEndDEnd())) {
//            sql2.append(" and  t.endD <= :endDEnd ");
//        }
        if(isNotEmpty(query.getCompany())) {
            sql2.append(" and  t.company like :company ");
            query.setCompany("%"+query.getCompany()+"%"); 

        }
        if(isNotEmpty(query.getCompanyEn())) {
            sql2.append(" and  t.companyEn like :companyEn ");
            query.setCompanyEn("%"+query.getCompanyEn()+"%"); 

        }
        if(isNotEmpty(query.getTitle())) {
            sql2.append(" and  t.title like :title ");
            query.setTitle("%"+query.getTitle()+"%"); 

        }
        if(isNotEmpty(query.getTitleEn())) {
            sql2.append(" and  t.titleEn like :titleEn ");
            query.setTitleEn("%"+query.getTitleEn()+"%"); 

        }
        if(isNotEmpty(query.getReportTitle())) {
            sql2.append(" and  t.reportTitle like :reportTitle ");
            query.setReportTitle("%"+query.getReportTitle()+"%"); 

        }
        if(isNotEmpty(query.getReportTitleEn())) {
            sql2.append(" and  t.reportTitleEn like :reportTitleEn ");
            query.setReportTitleEn("%"+query.getReportTitleEn()+"%"); 

        }
          if(isNotEmpty(query.getTeamSize())) {
            sql2.append(" and  t.teamSize = :teamSize ");
        }
        if(isNotEmpty(query.getProductSize())) {
            sql2.append(" and  t.productSize like :productSize ");
            query.setProductSize("%"+query.getProductSize()+"%"); 

        }
        if(isNotEmpty(query.getProductLevelEn())) {
            sql2.append(" and  t.productLevelEn like :productLevelEn ");
            query.setProductLevelEn("%"+query.getProductLevelEn()+"%"); 

        }
        if(isNotEmpty(query.getDutySize())) {
            sql2.append(" and  t.dutySize like :dutySize ");
            query.setDutySize("%"+query.getDutySize()+"%"); 

        }
        if(isNotEmpty(query.getDutySizeEn())) {
            sql2.append(" and  t.dutySizeEn like :dutySizeEn ");
            query.setDutySizeEn("%"+query.getDutySizeEn()+"%"); 

        }
        if(isNotEmpty(query.getDutyDesc())) {
            sql2.append(" and  t.dutyDesc like :dutyDesc ");
            query.setDutyDesc("%"+query.getDutyDesc()+"%"); 

        }
        if(isNotEmpty(query.getDutyDescEn())) {
            sql2.append(" and  t.dutyDescEn like :dutyDescEn ");
            query.setDutyDescEn("%"+query.getDutyDescEn()+"%"); 

        }
        if(isNotEmpty(query.getScore())) {
            sql2.append(" and  t.score like :score ");
            query.setScore("%"+query.getScore()+"%"); 

        }
        if(isNotEmpty(query.getScoreEn())) {
            sql2.append(" and  t.scoreEn like :scoreEn ");
            query.setScoreEn("%"+query.getScoreEn()+"%"); 

        }
        if(isNotEmpty(query.getLeaveResult())) {
            sql2.append(" and  t.leaveResult like :leaveResult ");
            query.setLeaveResult("%"+query.getLeaveResult()+"%"); 

        }
        if(isNotEmpty(query.getLeaveResultEn())) {
            sql2.append(" and  t.leaveResultEn like :leaveResultEn ");
            query.setLeaveResultEn("%"+query.getLeaveResultEn()+"%"); 

        }
        if(isNotEmpty(query.getCreator())) {
            sql2.append(" and  t.creator like :creator ");
            query.setCreator("%"+query.getCreator()+"%"); 

        }
        if(isNotEmpty(query.getCreateDtBegin())) {
            sql2.append(" and  t.createDt >= :createDtBegin ");
        }
        if(isNotEmpty(query.getCreateDtEnd())) {
            sql2.append(" and  t.createDt <= :createDtEnd ");
        }
        if(isNotEmpty(query.getModifier())) {
            sql2.append(" and  t.modifier like :modifier ");
            query.setModifier("%"+query.getModifier()+"%"); 

        }
        if(isNotEmpty(query.getModifiedDtBegin())) {
            sql2.append(" and  t.modifiedDt >= :modifiedDtBegin ");
        }
        if(isNotEmpty(query.getModifiedDtEnd())) {
            sql2.append(" and  t.modifiedDt <= :modifiedDtEnd ");
        }
        if(isNotEmpty(query.getSortColumns())) {
            sql2.append(" order by :sortColumns ");
            
        }else{
        	 sql2.append(" order by t.beginD desc ");
        }	
        
		return pageQuery(sql2.toString(),query);
	}

	public CvWork findByProperty(String propertyName, Object value) {
		return super.findByProperty(propertyName, value);
	}

	
	public List<CvWork> findAllBy(String propertyName, Object value) {
		return super.findAllBy(propertyName, value);
	}

	
	public void checkResumeDate(CvWork cvWork) {
		Integer cvId = cvWork.getCvId();
		if(cvId != null && !"".equals(cvId)){
			List<CvWork> cvList = this.getHibernateTemplate().find("from CvWork cv where cv.cvId=?", cvId);
			if(cvList.size() ==  0){
				List values = new ArrayList();
				 String sql = "update cv set resume_creator =?,resume_dt= ? ,cv_status='F' where id=?";
				 values.add(cvWork.getCreator());
				 values.add(cvWork.getCreateDt());
				 values.add(cvId);
				 int count = this.getJdbcDao().update(sql, values.toArray());
				 System.out.println("更新成功.");
			}
		}
	}

	
	/**
	 * 验证工作经历中的开始工作日期 不的早于前一份工作的结束日期
	 */
	public boolean validationWorkStartDate(String startD,String cvId) {
		StringBuffer sql = new StringBuffer();
		List<String> values = new ArrayList<String>();
		sql.append("select count(1) from cv_work a where a.cv_id = ?  and (a.end_d > ? or a.end_d is  null );");
		values.add(cvId);
		values.add(startD);
		int count = this.getJdbcDao().getJdbcTemplate().queryForInt(sql.toString(), values.toArray());
		if(count>0){
			return false;
		}
		return true;
	}
	
	
	
	
	

}
